﻿<%
'''依赖层级:002
'''依赖文件:functions.asp, AshapoConfig.class.asp, AshapoPreg.class.asp, AshapoFso.class.asp
'''底层日志类
Class AshapoLogDriverFile
	'日志输出等级: Error < Warning < Info < Debug
	'日志文件路径, 内部使用fsoAshapo对象, 等级,格式,产生日志的页面
	Private s_path, s_fso, s_level, s_format, s_npage
	'Level的替代转化符,Path的替代转化符, Time的替代转化符
	Private s_lvp, s_pap, s_tmp
	
	'''仅单次有效的PATH
	Private s_pathOnce
	
	'''构造
	Private Sub Class_Initialize()
		s_path =""
		s_level = "INFOX"
		Set s_fso = New AshapoFso
		s_lvp = "{%level%}"
		s_pap = "{%path%}"
		s_tmp = "{%time%}"
		s_npage = Request.ServerVariables("SCRIPT_NAME")
		Dim t_s : t_s = Request.ServerVariables("QUERY_STRING")
		If t_s <> "" Then
			s_npage = s_npage & "?" & t_s
		End If
		s_format = "Time:[" & s_tmp & "] Level:[" & s_lvp & "] Path:[""" & s_pap & """]"
		s_pathOnce = ""
	End Sub

	'''析构
	Private Sub Class_Terminate()
		Set s_fso = Nothing
	End Sub
	
	'''格式化当前时间日期格式
	'p_t:格式化格式
	Private Function formatDt_(Byval p_t)
		Dim t_y : t_y = Year(Now())
		Dim t_m : t_m = Right("0" & Month(Now()), 2)
		Dim t_d : t_d = Right("0" & Day(Now()), 2)
		Select Case p_t
		Case 1
			Dim t_h : t_h = Right("0" & Hour(Now()), 2)
			Dim t_n : t_n = Right("0" & Minute(Now()), 2)
			Dim t_s : t_s = Right("0" & Second(Now()), 2)
			formatDt_ = t_y & "-" & t_m & "-" & t_d & " " & t_h & ":" & t_n & ":" & t_s
		Case 2
			formatDt_ = t_y & t_m & t_d
		End Select
	End Function
	
	'''为当前欲写入的日志设置等级
	'p_s:Error < Warning < Info < Debug
	Public Property Let Level(Byval p_s)
		Select Case Lcase(p_s)
		Case "info"
			s_level = "INFOX"
		Case "sql"
			s_level = "SQLXX"
		Case "warning"
			s_level = "WARNX"
		Case Else
			s_level = Ucase(p_s)
		End Select
	End Property
	
	'''设置输出的路径。如果是文件夹路径则自动建立以时间为基准的日志文件,否则按指定路径输出
	'p_p:文件或文件夹路径
	Public Property Let Path(Byval p_p)
	     s_path = p_p
	End Property
	
	'''获取日志输出的路径,写入业务日志时会用到
	Public Property Get Path()
		Path = s_path
	End Property
	
	'''设置单次输出路径,使用一次后路径自动改回原本的路径
	Public Function PathOnce(Byval p_p)
		s_pathOnce = p_p
	End Function
	
	'''自定义格式化日志
	'p_s:格式化字符,特征值:1\2\3\simple\middle\full
	Public Property Let [Format](Byval p_s)
		Select Case Lcase(Cstr(p_s))
		Case "1","simple"
			s_format = "Level:[" & s_lvp & "]"
		Case "2","middle"
			s_format = "Time:[" & s_tmp & "] Level:[" & s_lvp & "]"
		Case "3","full"
			s_format = "Time:[" & s_tmp & "] Level:[" & s_lvp & "] Path:[""" & s_pap & """]"
		Case Else
			s_format = p_s
		End Select
	End Property
	
	'''(更新/创建日志文件)
	'p_p:日志路径,如果是文件夹路径则自动建立以时间为基准的日志文件,否则按指定路径输出
	'p_s:单行日志内容
	Private Sub createAfile_(Byval p_p, Byval p_s)
		p_p  = Replace(p_p,"\","/")
		If InStrRev(p_p,".") < InStrRev(p_p,"/") Then
			p_p = p_p & "/" & formatDt_(2) & ".log"
		End If
		p_p  = Replace(p_p,"//","/")
		If s_fso.IsFile(p_p) Then
			Call s_fso.AppendFile(p_p, p_s & vbcrlf)
		Else
			Call s_fso.CreateFile(p_p, p_s & vbcrlf, True)
		End If
	End Sub
	
	'写入日志。总入口，其余扩展请基于本函数实现
	'p_s:日志实体,不含前缀格式化
	Public Sub [Write](Byval p_s)
		Dim t_t : t_t = s_format
	    t_t = Replace(t_t, s_lvp, s_level)
		t_t = Replace(t_t, s_pap, s_npage)
		t_t = Replace(t_t, s_tmp, formatDt_(1))
		If s_pathOnce = "" Then
			Call createAfile_(s_path, t_t&" "&p_s)
		Else
			Call createAfile_(s_pathOnce, t_t&" "&p_s)
			s_pathOnce = ""
		End If
	End Sub
End Class
%>